package algorithm;

import java.util.Arrays;

/**
 * 66. 加一
 * @author Panbo Guo
 * @date 2021/7/31
 */
public class T00066 {

    public static void main(String[] args) {
        int[] arr = {1,2,3};
        System.out.println(Arrays.toString(new Solution().plusOne(arr)));
    }

    static class Solution {
        public int[] plusOne(int[] digits) {
            int over = 1, index = digits.length - 1;
            while (index >= 0) {
                if (over > 0) {
                    digits[index] += over;
                    if (digits[index] > 9) {
                        digits[index] = 0;
                        over = 1;
                    } else {
                        over = 0;
                    }
                }
                index --;
            }
            if (over > 0) {
                int[] newArr = new int[digits.length + 1];
                newArr[0] = over;
                System.arraycopy(digits, 0, newArr, 1, digits.length);
                return newArr;
            }
            return digits;
        }
    }
}
